import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
const Login = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Login.vue')
// import Home from '../components/Home.vue'
const Home = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Home.vue')
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Welcome.vue')

// import User from '../components/users/User.vue'
const User = () => import(/* webpackChunkName: "User" */ '../components/users/User.vue')

// import Rights from '../components/power/Rights.vue'
const Rights = () => import(/* webpackChunkName: "Rights_Roles" */ '../components/power/Rights.vue')
// import Roles from '../components/power/Roles.vue'
const Roles = () => import(/* webpackChunkName: "Rights_Roles" */ '../components/power/Roles.vue')

// import GoodsCate from '../components/goods/Goods_cate.vue'
const GoodsCate = () => import(/* webpackChunkName: "GoodsCate_Params_List_Add" */ '../components/goods/Goods_cate.vue')
// import Params from '../components/goods/Goods_params.vue'
const Params = () => import(/* webpackChunkName: "GoodsCate_Params_List_Add" */ '../components/goods/Goods_params.vue')
// import List from '../components/goods/Goods_list.vue'
const List = () => import(/* webpackChunkName: "GoodsCate_Params_List_Add" */ '../components/goods/Goods_list.vue')
// import Add from '../components/goods/Goods_add.vue'
const Add = () => import(/* webpackChunkName: "GoodsCate_Params_List_Add" */ '../components/goods/Goods_add.vue')

// import Order from '../components/order/Order.vue'
const Order = () => import(/* webpackChunkName: "Order" */ '../components/order/Order.vue')

// import Report from '../components/report/Report.vue'
const Report = () => import(/* webpackChunkName: "Report" */ '../components/report/Report.vue')

Vue.use(VueRouter)

const routes = [{
path: '/login',
component: Login
},
{
path: '',
redirect: '/login'
},
{
path: '/home',
component: Home,
redirect: '/welcome',
children: [
{ path: '/users', component: User },
{ path: '/welcome', component: Welcome },
{ path: '/rights', component: Rights },
{ path: '/roles', component: Roles },
{ path: '/categories', component: GoodsCate },
{ path: '/params', component: Params },
{ path: '/goods', component: List },
{ path: '/goods/add', component: Add },
{ path: '/orders', component: Order },
{ path: '/reports', component: Report }
]
}
]

const router = new VueRouter({
routes
})

// 使用路由导航守卫，进行权限控制 当没有token的情况下，访问home以及后面的组件时候，强制跳转到login组件（访问api是需要权限的）
// beforeEach方法中 to 表示要访问的路径 from 表示从哪里来 next 表示下一步要去哪里（如果符合就放行，不符合就强制跳转）
router.beforeEach((to, from, next) => {
if (to.path === '/login') return next()
// 获取sessionStorage中的token
const token = sessionStorage.getItem('token')
// console.log(token)
// 在进行条件判断
// 如果token为空
if (!token) {
// 强制跳转到登录组件
return next('/login')
}
// 放行
next()
})
export default router
